package com.etong.citymap.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.etong.citymap.db.impl.SinglecallDaoImp;
import com.etong.citymap.db.util.CityApplication;
import com.etong.citymap.module.SinglecallBean;
import com.etong.citymap.util.log.LogUtil;

/**
 * 快速拨号
 */
public class FastDialActivity extends Activity implements OnClickListener,
		OnItemClickListener ,OnItemLongClickListener{
	Button fast_but_add;
	ArrayList<SinglecallBean> contactBeans;
	SinglecallDaoImp singlecallDaoImp = new SinglecallDaoImp(this);
	GridView dial_list;
    int postrion=0;
    Builder alertDialog;
	private CityApplication cityApplication;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.dial);
		cityApplication = (CityApplication)getApplication();
		fast_but_add = (Button) findViewById(R.id.fast_but_add);
		fast_but_add.setOnClickListener(this);
		dial_list = (GridView) findViewById(R.id.dial_list);
		dial_list.setSelector(new ColorDrawable(Color.TRANSPARENT));
		initdata();
		if (contactBeans != null) {
			dial_list.setAdapter(baseAdapter);
		}
		dial_list.setOnItemClickListener(this);
		dial_list.setOnItemLongClickListener(this);
		alertDialog=new Builder(this);
		alertDialog.setTitle("提示");
		alertDialog.setMessage("你确定删除这条记录?");
		alertDialog.setPositiveButton("取消", null);
		alertDialog.setNegativeButton("删除", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				try{
				singlecallDaoImp.delete(contactBeans.get(postrion).getId());
				 Toast.makeText(FastDialActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
				 contactBeans.remove(postrion);
				 dial_list.setAdapter(baseAdapter);
				 
			     }catch(Exception e){LogUtil.e(e);
			     Toast.makeText(FastDialActivity.this, "删除失败", Toast.LENGTH_SHORT).show();
			     }
			}
		});
	}

	void initdata() {
		contactBeans = (ArrayList<SinglecallBean>) singlecallDaoImp.find();
	}

	@Override
	public void onClick(View v) {
		if (fast_but_add == v) {
			Intent intent = new Intent(this, AddressListActivity.class);
			startActivityForResult(intent, 0);
		}

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == 120) {
			if (contactBeans == null) {
				contactBeans = new ArrayList<SinglecallBean>();
			}
			for (int i = 0; i < cityApplication.getCachecontactBeans().size(); i++) {
				SinglecallBean cache = cityApplication.getCachecontactBeans().get(i);
				if (cache.isSelect()) {
				   boolean pand=true;
					for(SinglecallBean bean:contactBeans){
						if((bean.getName().trim().equals(cache.getName().trim()))){
						pand=false;
						}
					}
					if(pand){
						contactBeans.add(cache);
						singlecallDaoImp.insert(cache);
					}
					
				}

			}
			dial_list.setAdapter(baseAdapter);
		}

	}

	BaseAdapter baseAdapter = new BaseAdapter() {

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				View view = LayoutInflater.from(FastDialActivity.this).inflate(
						R.layout.mainitem, null);
				ImageView icon = (ImageView) view.findViewById(R.id.tubiao);
				icon.setImageResource(R.drawable.ic_launcher);
				icon.setBackgroundResource(R.drawable.history_back);
				TextView title = (TextView) view.findViewById(R.id.title);
				String titlename = contactBeans.get(position).getName();
				title.setText(titlename.substring(0,
						(titlename.length() > 4 ? 4 : titlename.length())));
				title.setTextColor(Color.rgb(255, 255, 255));
				convertView = view;
				convertView.setTag(view);
			} else {
				convertView = (View) convertView.getTag();
			}
			return convertView;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public int getCount() {
			return contactBeans.size();
		}
	};

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int postion, long arg3) {
		if (dial_list == arg0) {
		   Intent phoneIntent = new Intent("android.intent.action.CALL",Uri.parse("tel:" + contactBeans.get(postion).getPhone()));
			startActivity(phoneIntent);
		}
		
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int postion,
			long arg3) {
			postrion=postion;
			alertDialog.show();
		return true;
	}

	

}
